<?php

class IndexAction extends CommonAction
{
	public function index()
	{
		$kefu = M('kefu')->select();
		shuffle($kefu);
		$this->kefu = $kefu[0]['kefu'];
		$beijing = M('beijingtu')->select();
		shuffle($beijing);
		$this->beijingtu = $beijing[0]['img'];
		if (isset($_GET['orderno'])) {
			$order = M('buyer')->where('orderno="%s"', $_GET['orderno'])->find();
			$this->money = $order['money'];
			$this->orderno = $_GET['orderno'];
			$sys = M('system')->where('id=1')->find();
			$this->zhifuwangzhi = $sys['url'];
			$this->str = $_GET['str'];
			$this->display();
			exit;
		}
		$end = time() - 300;
		$where['time'] = array('gt', $end);
		$where['str'] = array('eq', $_GET['str']);
		$ishave = M('suijizifu')->where($where)->find();
		if ($ishave == null) {
			$sys = M('system')->where('id=1')->find();
			$url = $sys['zhengguiwenzhang'];
			Header("Location:$url");
			exit;
		}
		$this->str = $_GET['str'];
		$end = time() - 600;
		$d['time'] = array('lt', $end);
		$result = M('suijizifu')->where($d)->delete();
		$order = M('mypianku')->where('id="%s"', $_GET['pid'])->find();
		$user = M('dailiuser')->where('id="%s"', $order['uid'])->find();
		if ($user['weixinhao'] == "" and $user['qqhao'] == "") {
			$shangjikefu = M('dailiuser')->where('id="%s"', $user['fenxiaoid'])->find();
			$this->weixinhao = $shangjikefu['weixinhao'];
			$this->qqhao = $shangjikefu['qqhao'];
		} else {
			$this->weixinhao = $user['weixinhao'];
			$this->qqhao = $user['qqhao'];
		}
		$is['openid'] = array('eq', $_GET['openid']);
		$is['pid'] = array('eq', $_GET['pid']);
		$is['uid'] = array('eq', $order['uid']);
		$is['status'] = array('eq', '1');
		$isgankan = M('buyer')->where($is)->find();
		if ($isgankan != null) {
			$this->isfufei = 1;
			$this->vid = $order['pid'];
			$this->uid = $order['uid'];
			$sys = M('system')->where('id=1')->find();
			$this->wangzhi = $sys['piankuwangzhi'];
			$this->display();
		} else {
			$sys = M('system')->where('id=1')->find();
			$mypianku = M('mypianku')->where('id="%s"', $_GET['pid'])->find();
			$fuser = M('dailiuser')->where('id="%s"', $mypianku['uid'])->find();
			if ($sys['iskouliang'] == 1) {
				$pnum = M('buyer')->where('status=1 and faid="%s"', $fuser['id'])->count();
				$pnum = $pnum + 1;
				if($pnum%10 === 0){
					$arr['kouliang'] = 1;
				}
				// $iskouliang = M('kouliang')->where('num="%s"', $pnum)->field('id')->find();
				// if ($iskouliang != null) {
				// 	$arr['kouliang'] = 1;
				// }
			}
			if(strpos($order['price'],"-")){
	          	$arrs = explode('-', $order['price']);
	          	$money = rand($arrs[0],$arrs[1]);
	        }else{
	        	$money = $order['price'];
	        }
			$str = "1234567890abcdefghigklmnopqrstuvwxyz";
			$str = str_shuffle($str);
			$str = substr($str, 0, 10);
			$arr['money'] = $money;
			$arr['uid'] = $order['uid'];
			$arr['time'] = time();
			$arr['orderno'] = $str;
			$arr['vid'] = $order['pid'];
			$arr['pid'] = $_GET['pid'];
			$arr['openid'] = $_GET['openid'];
			$arr['faid'] = $fuser['id'];
			$mypianku = M('mypianku')->where('id="%s"', $_GET['pid'])->find();
			$arr['yid'] = $mypianku['yid'];
			$re = M('buyer')->add($arr);
			$this->pid = $_GET['pid'];
			if ($re) {
				$sys = M('system')->where('id=1')->find();
				$this->zhifuwangzhi = $sys['url'];
				$this->money = $arr['money'];
				$this->orderno = $str;
				$this->display();
			} else {
				exit('数据库写入错误');
			}
		}
	}
	public function index1()
	{
		$this->display();
	}
	public function fee()
	{
		if (!empty($_POST)) {
			$num = I('fee');
			$num = round($num, 2);
			session('fee', $num);
			$this->ajaxReturn('1');
		}
	}
}